<?php
require_once 'readmains_controller.php';
class SinhviensController extends AppController {

	var $name = 'Sinhviens';
	var $components = array('FileUpload.FileUpload');
	var $uses = array('Sinhvien','Nganh','Lop');
	
	function index() {
		$this->Sinhvien->recursive = 0;
		$this->set('sinhviens', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid sinhvien', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('sinhvien', $this->Sinhvien->read(null, $id));
	}

	function add() {
		if (!empty($this->data))
		{
			$this->Sinhvien->create();
			$this->data['Sinhvien']['ngaySinhSV'] =date('Y-m-d', strtotime($this->data['Sinhvien']['ngaySinhSV']));
			if ($this->Sinhvien->save($this->data)) {
				$this->Session->setFlash(__('The sinhvien has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The sinhvien could not be saved. Please, try again.', true));
			}
		}
		$this->Sinhvien->Lop->displayField='tenLop';
		$lops = $this->Sinhvien->Lop->find('list');
		$this->set(compact('lops'));
	}

	function edit($id = null) {
		debug($this->data);
		if (!$id && empty($this->data)) {
			
			$this->Session->setFlash(__('Invalid sinhvien', true));
			$this->redirect(array('action' => 'index'));
			
		}
		
		if (!empty($this->data)) {
			$this->data['Sinhvien']['ngaySinhSV']=date('Y-m-d',strtotime($this->data['Sinhvien']['ngaySinhSV']));
			if ($this->Sinhvien->save($this->data)) {
				$this->Session->setFlash(__('The sinhvien has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				
				$this->Session->setFlash(__('The sinhvien could not be saved. Please, try again.', true));
			}
		}
		
		if (empty($this->data)) {
			
			$this->data = $this->Sinhvien->read(null, $id);
			$this->data['Sinhvien']['ngaySinhSV']=date('d-m-Y', strtotime($this->data['Sinhvien']['ngaySinhSV']));
			//debug($this->data);
		}
		
		$this->Sinhvien->Lop->displayField='tenLop';
		$lops = $this->Sinhvien->Lop->find('list');
		$this->set(compact('lops'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for sinhvien', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Sinhvien->delete($id)) {
			$this->Session->setFlash(__('Sinhvien deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Sinhvien was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function upload()
	{
		if(!empty($this->data))
		{
			
			$er=$this->FileUpload->showErrors();
			if($this->FileUpload->success||empty($er))
			{
				$err = $this->__impTodb(WWW_ROOT.DS.'files'.DS.$this->data['Sinhvien']['file']['name']);
				if($err)
					$this->redirect(array('action'=>'upload'),'Lỗi Import');
				else
					$this->redirect(array('action'=>'upload'),'Import Thành Công');
					
				@unlink(WWW_ROOT.DS.'files'.DS.$this->data['Sinhvien']['file']['name']);
			}
		 else{
                $this->Session->setFlash($er);
            }
		}
	}
	
/**
     * __impTodb($newfilename, $client_filename)
     * 
     * HÃ m nÃ y Ä‘á»�c dá»¯ liá»‡u tá»« 1 file excel (read content of excel file)
     * LÆ°u trá»¯ dá»¯ liá»‡u Ä‘á»�c Ä‘Æ°á»£c vÃ o csdl, lÆ°u theo tá»«ng dÃ²ng trÃªn excel (save to DB, save each row)
     *  
     * @param mixed $newfilename Ä‘Æ°á»�ng dáº«n Ä‘áº¿n file excel trÃªn server (the patch of excel on server)
     * @param mixed $client_filename tÃªn cá»§a file excel lÃºc táº£i lÃªn (old filename before upload)
     *          
     * @return number (==0 if succeed, otherwise there are 7 error cases)
     * 
     */ 
   function __impTodb($filename)
   {
   		$err=0;
   		set_time_limit(300);
        //doc du lieu tu tap tin tren server
        $data = new Spreadsheet_Excel_Reader($filename,true,'UTF-8');
        $dulieus = $data->dump(true,true,0);
        
        $nganhs = array();
        $lops = array();
        $this->Nganh->displayField='tenNganh';
        $nganhdacos = $this->Nganh->find('list');
        
        $this->Lop->displayField='malop';
        $lopdacos = $this->Lop->find('list');
   		foreach($nganhdacos as $key => $nganhdaco)
        {
        	$nganhdacos[$key] = strtolower($nganhdaco);
        }
        foreach($dulieus as $dulieu)
        {
        	if(!in_array(strtolower($dulieu[2]),$nganhdacos))
        		if(!in_array(strtolower($dulieu[2]), $nganhs) && !empty($dulieu[2]))
        		{
        			/*$temp = array();
        			$temp['khoa_id'] = 1;
        			$temp['tenNganh'] = strtolower($dulieu[2]);*/
        			
        			$nganhs[] =strtolower($dulieu[2]);
        		}
        			
        	if(!in_array(strtoupper($dulieu[4]),$lopdacos))
        		if(!in_array(strtoupper($dulieu[4]).'_'.$dulieu[5].'_'.$dulieu[2], $lops)&& !empty($dulieu[4]))
        		{
        			/*$temp = array();
        			$temp['malop'] = strtolower($dulieu[4]);
        			$temp['tenLop'] = $dulieu[5];*/
        			$lops[] = strtoupper($dulieu[4]).'_'.$dulieu[5].'_'.$dulieu[2];
        		}
       }
       /// them nganh va khoa hoc 
       $this->Nganh->create();
       foreach ($nganhs as $nganh)
       {
       		$recordng['Nganh']['tenNganh']= $nganh;
       		$ngnh = $nganh;
       		if(!$this->Nganh->saveAll($recordng))
       			$err=1;
       }
       $nganhdacos = $this->Nganh->find('list',array('fields'=>array('tenNganh','id')));
       $this->Lop->create();
       foreach ($lops as $lop)
       {
       		$tmp = split('_',$lop);
       		$recordlop['Lop']['nganh_id']= $nganhdacos[$tmp[2]];
       		$recordlop['Lop']['malop']= $tmp[0];
       		$recordlop['Lop']['tenLop']= $tmp[1];
       		if(!$this->Lop->saveAll($recordlop))
       			$err=1;
       }
        $lopdangcos = $this->Lop->find('list',array('fields'=>array('malop','id')));
        
        foreach ( $dulieus as $sinhvien)
        {
        	if(isset($lopdangcos[$sinhvien[4]]))
        	{
        		$dg = $this->Sinhvien->find('list',array('conditions'=>array('mssv'=>$sinhvien[6])));
        		if(empty($dg))
        		{
		        	$recorddg['Sinhvien']['mssv']=$sinhvien[6];
		        	$recorddg['Sinhvien']['tenSV']=$sinhvien[7];
		        	$recorddg['Sinhvien']['gioiTinhSV']=$sinhvien[12];
		        	$recorddg['Sinhvien']['ngaySinhSV']=date('Y-m-d',strtotime($sinhvien[8]));
		        	$recorddg['Sinhvien']['lop_id']=$lopdangcos[$sinhvien[4]];
		        	if(!$this->Sinhvien->saveAll($recorddg))
		        	{
		        		$err=1;
		        	}
        		}
        	}
        }
        return $err;
   }
}
